package com.besall.allbase.bluetooth.scan;

import java.util.ArrayList;
import java.util.List;

import com.bes.bessdk.connect.UsbConnector;
import com.bes.bessdk.scan.BesScanManager;
import com.bes.sdk.scan.ScanManager;
import com.bes.sdk.utils.DeviceProtocol;
import com.besall.allbase.common.manager.PermissionManager;
import com.besall.allbase.view.base.BasePresenter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;

public class ScanPresenter extends BasePresenter<IScanActivity> implements IScanPresenter {
    private final String TAG = "ScanPresenter";
    private List<DeviceProtocol> mList = new ArrayList<>();
    private ScanManager mScanner;
    private int onGrantedCount = 0;

    @Override
    public void starScanWithScanType(Context context, DeviceProtocol deviceProtocol) {
        Log.i(TAG, "starScanWithScanType: ----------------1111111111");
        if (deviceProtocol == DeviceProtocol.PROTOCOL_USB) {
            UsbConnector.scanDevice(context, (ScanManager.ScanListener) context);
            return;
        }
        Log.d(TAG, " begin starScanWithScanType");
        String[] permissions;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            permissions = new String[] { Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_ADVERTISE,
                    Manifest.permission.BLUETOOTH_CONNECT };
        } else {
            permissions = new String[] { Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION };
        }
        PermissionManager.getInstance().requestPermissions((Activity) context,
                new PermissionManager.PermissionUtilListener() {
                    @Override
                    public void onGranted() {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
                            onGrantedCount++;
                            if (onGrantedCount < 2) {
                                Log.d(TAG, "  starScanWithScanType,onGrantedCount < 2 return");
                                return;
                            }
                        }

                        if (mScanner != null) {
                            mScanner.stopScan(mList);
                            mScanner = null;
                        }
                        mList.clear();
                        mList.add(deviceProtocol);
                        mScanner = new BesScanManager(context);
                        Log.d(TAG, "  starScanWithScanType,startScan");
                        mScanner.startScan(mList, (ScanManager.ScanListener) context, null);
                    }

                    @Override
                    public void onUngranted(String msg) {
                        Log.i(TAG, "onUngranted: ");
                    }

                    @Override
                    public void onError(String msg) {
                        Log.i(TAG, "onError: ");
                    }
                }, permissions);

    }

    @Override
    public void stopScanWithScanType(Context context, DeviceProtocol deviceProtocol) {
        if (mScanner != null) {
            mScanner.stopScan(mList);
            mScanner = null;
        }
    }

}
